
package fr.mehfile.api.parser;

import fr.mehfile.api.database.NewsFromDatabase;
import fr.mehfile.api.database.ProgramFromDatabase;
import fr.mehfile.api.database.ScheduleFromDatabase;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

public class UpdateData {
    private int xmlVersion = -1;
    private URL serverUrl;

    private Programs programs;
    private News news;

    public Programs getPrograms() {
        return programs;
    }

    public void setPrograms(Programs programs) {
        this.programs = programs;
    }

    public News getNews() {
        return news;
    }

    public void setNews(News news) {
        this.news = news;
    }

    public int getXmlversion() {
        return xmlVersion;
    }

    public void setXmlversion(int xml_version) {
        this.xmlVersion = xml_version;
    }

    public URL getServerUrl() {
        return serverUrl;
    }

    public void setServerUrl(URL serverUrl) {
        this.serverUrl = serverUrl;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer("");
        buffer.append("xmlVersion=" + xmlVersion);
        buffer.append("\n");
        buffer.append("serverUrl=" + serverUrl);
        buffer.append("\n");
        if (programs != null) {
            ArrayList<Program> program = programs.getProgram();
            Iterator<Program> programIterator = program.iterator();
            Program currentProgram;
            while (programIterator.hasNext()) {
                currentProgram = programIterator.next();

                buffer.append("date=" + currentProgram.getDate());
                buffer.append("\n");
                buffer.append("title=" + currentProgram.getTitle());
                buffer.append("\n");
                buffer.append("description=" + currentProgram.getDescription());
                buffer.append("\n");

                Schedules schedules = currentProgram.getSchedules();
                ArrayList<Schedule> schedule = schedules.getSchedule();
                Iterator<Schedule> scheduleIterator = schedule.iterator();
                Schedule currentSchedule;
                while (scheduleIterator.hasNext()) {
                    currentSchedule = scheduleIterator.next();

                    buffer.append("start_time=" + currentSchedule.getStart_time());
                    buffer.append("\n");
                    buffer.append("end_time=" + currentSchedule.getEnd_time());
                    buffer.append("\n");
                    buffer.append("title=" + currentSchedule.getTitle());
                    buffer.append("\n");
                }
            }
        }

        if (news != null) {
            ArrayList<CurrentNews> listOfNews = news.getCurrentNews();
            Iterator<CurrentNews> currentNewsIterator = listOfNews.iterator();
            CurrentNews currentNews;
            while (currentNewsIterator.hasNext()) {
                currentNews = currentNewsIterator.next();

                buffer.append("title=" + currentNews.getTitle());
                buffer.append("\n");
                buffer.append("content=" + currentNews.getContent());
                buffer.append("\n");
                buffer.append("image=" + currentNews.getImage());
                buffer.append("\n");

            }
        }
        return buffer.toString();
    }

    public boolean isProgramInUpdate(ProgramFromDatabase currentProgramFromDatabase) {

        ArrayList<Program> program = programs.getProgram();
        Program currentProgram;

        for (int i = 0; i < program.size(); i++) {
            currentProgram = program.get(i);
            if (currentProgram.getTitle().compareTo(currentProgramFromDatabase.getTitle()) == 0
                        &&
                        currentProgram.getDate().compareTo(currentProgramFromDatabase.getDate()) == 0
                        &&
                        currentProgram.getDescription().compareTo(
                                currentProgramFromDatabase.getDescription()) == 0) {
                if (isScheduleInUpdate(currentProgramFromDatabase.getSchedules(),
                            currentProgram.getSchedules().getSchedule())) {
                    return true;
                }
            }
        }

        return false;
    }

    public boolean isScheduleInUpdate(ArrayList<ScheduleFromDatabase> scheduleFromDatabase,
            ArrayList<Schedule> scheduleFromUpdate) {

        if (scheduleFromUpdate.size() != scheduleFromUpdate.size())
            return false;

        ScheduleFromDatabase currentScheduleFromDatabase = null;
        Schedule currentScheduleFromUpdate = null;
        for (int i = 0; i < scheduleFromUpdate.size(); i++) {
            currentScheduleFromUpdate = scheduleFromUpdate.get(i);
            currentScheduleFromDatabase = scheduleFromDatabase.get(i);
            
            if (currentScheduleFromUpdate.getTitle().compareTo(
                            currentScheduleFromDatabase.getTitle()) == 0
                            &&
                            currentScheduleFromUpdate.getStart_time().compareTo(
                                    currentScheduleFromDatabase.getStart_time()) == 0
                            &&
                            currentScheduleFromUpdate.getEnd_time().compareTo(
                                    currentScheduleFromDatabase.getEnd_time()) == 0) {

          
                return true;
            }

        }

        return false;
    }

    public boolean isCurrentNewsInUpdate(NewsFromDatabase currentNewsFromDatabase) {

        ArrayList<CurrentNews> listOfNews = news.getCurrentNews();
        CurrentNews currentNews;
        for (int i = 0; i < listOfNews.size(); i++) {
            currentNews = listOfNews.get(i);
            if (currentNews.getTitle().compareTo(currentNewsFromDatabase.getTitle()) == 0
                        &&
                        currentNews.getImage().toString()
                                .compareTo(currentNewsFromDatabase.getImageUrl().toString()) == 0
                        &&
                        currentNews.getContent().compareTo(currentNewsFromDatabase.getContent()) == 0)
                return true;
        }

        return false;

    }

}
